<?php


namespace app\hotel\controller;


use app\BaseController;
use app\Code;
use app\hotel\validate\AuthValidate;
use app\lib\exception\WxApiException;
use app\service\AuthService;
use think\App;
use think\exception\ValidateException;

class AuthController extends BaseController
{

    /**
     * @var AuthService
     */
    private $service;

    public function __construct(App $app)
    {
        $this->service = new AuthService();
        parent::__construct($app);
    }

    public function wxLogin(){
        $param = $this->request->param(['js_code', 'username', 'head_img']);

        validate(AuthValidate::class)
            ->scene('WxLogin')
            ->check($param);

        $result = $this->service->wxLogin($param);

        return $this->returnJson(['token' => $result['token'], 'user' => $result['user']]);
    }

    public function sendSms(){
        $param = $this->request->param(['phone']);

        validate(AuthValidate::class)
            ->scene('sendSms')
            ->check($param);

        $this->service->sendSms($param['phone'],  $this->request->user->id);

        return $this->returnJson( $this->service->validateSms($param['phone'],  $this->request->user->id));
    }

    public function validateSms(){
        $param = $this->request->param(['code']);

        return $this->returnJson( $this->service->validateSms($param['code'], $this->request->user->id));
    }

    public function validateIDCard(){
        $IDCard = $this->request->param('idcard');

        return $this->returnJson( $this->service->validateIDCard($IDCard));
    }
}
